package com.bjpowernode.service;

import com.bjpowernode.dao.GoodsDao;
import com.bjpowernode.dao.SaleDao;
import com.bjpowernode.entity.Goods;
import com.bjpowernode.entity.Sale;
import com.bjpowernode.exception.NotEnoughException;

public class BuyGoodsServiceImpl implements BuyGoodsService {

    private GoodsDao goodsDao;
    private SaleDao saleDao;
    public void setGoodsDao(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
    }
    public void setSaleDao(SaleDao saleDao) {
        this.saleDao = saleDao;
    }

    @Override
    public void buy(Integer goodsId, Integer nums) {
        //纪录销售信息。向sale表中添加纪录
        Sale sale=new Sale();
        sale.setGid(goodsId);
        sale.setNums(nums);
        saleDao.addSale(sale);

        //更新库存
        Goods goods=goodsDao.queryAllGoods(goodsId);
        if(goods==null){
            throw new NullPointerException("编号是"+goodsId+"的商品不存在");
        }
        if(goods.getAmount()<nums){
            throw new NotEnoughException("编号是"+goodsId+"的商品库存不足");
        }

        Goods goods1=new Goods();
        goods1.setId(goodsId);
        goods1.setAmount(nums);
        goodsDao.reduceAmount(goods1);


        System.out.println("方法执行完毕");
    }

    public void query(Integer goodsId){
        Goods goods=goodsDao.queryAllGoods(goodsId);
        System.out.println(goods.toString());
    }
}
